<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <meta>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>知云——针对编程的个性化学习推荐系统</title>
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath());
    %>
    <base href="${APP_PATH}">
    <c:if test="${empty admin}">
        <%--非法访问--%>
        <c:redirect url="${APP_PATH}/adminLogin"/>
    </c:if>
    <link rel="shortcut icon" type="image/x-icon" href="${APP_PATH}/static/favicon.ico"/>
    <link rel="stylesheet" href="${APP_PATH}/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/toastr.min.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/offcanvas.css">
    <link rel="stylesheet" href="${APP_PATH}/static/css/fontawesome.min.css">


</head>
<body>

<jsp:include page="${APP_PATH}/common/admin/head-report.jsp"/>
<div class="col-sm-12 col-md-12 col-lg-9" style="margin-left: auto;margin-right: auto;">
    <div class="my-2 p-3 bg-white rounded d-flex">
        <div class="flex-fill text-center"><a href="/adminReport" class="text-dark">博客</a></div>
        <div class="flex-fill text-center"><a href="/adminReport-question" class="text-dark">问题</a></div>
        <div class="flex-fill text-center"><a href="#" class="text-danger">回答</a></div>
    </div>

    <div class="my-2 p-3 bg-white rounded">
        <div id="items"></div>
    </div>
    <div id="pagenav"></div>
</div>
<div class="p-4"></div>

<script src="${APP_PATH}/static/js/jquery-3.3.1.min.js"></script>
<script src="${APP_PATH}/static/js/bootstrap.min.js"></script>
<script src="${APP_PATH}/static/js/toastr.min.js"></script>
<script src="${APP_PATH}/static/js/offcanvas.js"></script>

<script>
    function delAnswer(ele) {
        var answerId = $(ele).attr("data-answerId");
        if (answerId) {
            $.ajax({
                url: '/delAnswer/{answerId}',
                type: 'POST',
                success: function (result) {
                    if (result.code === 100) {
                        $("div[data-flag-answerId="+answerId+"]").remove();
                    }else{
                        toastr.error("操作失败，请稍后重试！");
                    }
                }
            });
        }
    }

    function read(ele) {
        var reportId = $(ele).attr("data-reportId");
        $.ajax({
            url: '/report/read/' + reportId,
            type: 'GET',
            success: function (result) {
                if (result.code === 100) {
                    $("a[data-flag-reportId=" + reportId + "]").remove();
                } else {
                    toastr.error("操作失败，请稍后重试!");
                }
            }
        });
    }

    function buildPage(result) {
        var $items = $("#items");
        $items.empty();
        $.each(result.extend.pageInfo.list, function (index, item) {
            if (item.answer) {
                var $div_media = $("<div data-flag-answerId='"+item.reportfieldid+"' class='media pt-3'>");
                var $img = $("<img alt='img' src='" + item.headimgurl + "' style='object-fit: cover' data-holder-rendered='true' width='40px' height='40px'>");
                var $p = $("<div class='media-body ml-3 pb-3 mb-0 lh-125 border-bottom border-gray'>");
                var $strong = $("<strong class='d-block text-gray-dark'>" + item.nickname + "</strong>");
                $div_media.append($img).append($p.append($strong).append(item.answer.answercontent));
                //已读按钮
                if (item.hasread === 0) {
                    var $div_footer = $("<div class='small text-right'><a data-flag-reportId='" + item.id + "' data-reportId='" + item.id + "' onclick='read(this)' style='cursor:pointer'>标为已读&nbsp;&nbsp;</a><a onclick='delAnswer(this)' data-answerId='" + item.reportfieldid + "' class='text-dark' style='cursor:pointer'>删除该回答</a></div>");
                    $p.append($div_footer);
                }
                $div_media.appendTo($items);
            }
        })
    }

    function buildPage_pagenav(result) {
        var pagenav = $("#pagenav");
        pagenav.empty();

        var div = $("<div class='p-2'>");
        var ul = $("<ul></ul>").addClass("pagination");
        //遍历给ul中添加页码提示
        $.each(result.extend.pageInfo.navigatepageNums, function (index, item) {

            var numLi = $("<li class='page-item'></li>").append($("<a class='btn btn-sm btn-outline-primary mr-2'></a>").append(item));
            if (result.extend.pageInfo.pageNum === item) {
                //当前页按钮，不可点击
                numLi.find("a").removeClass("btn-outline-primary");
                numLi.find("a").addClass("disabled");
                numLi.find("a").addClass("btn-primary");
            }
            numLi.click(function () {
                toPage(item);
            });
            ul.append(numLi);
        });
        div.append(ul).appendTo(pagenav);
    }

    function toPage(pn) {
        $.ajax({
            url: '/report/3/' + pn,
            type: 'GET',
            success: function (result) {
                if (result.code === 100) {
                    if (result.extend.pageInfo.list.length > 0) {
                        buildPage(result);
                        buildPage_pagenav(result);
                    } else {
                        toastr.info("暂无该类别的举报信息！");
                    }
                } else {
                    toastr.error(result.extend.error);
                }
            }
        });
    }

    $(function () {
        toPage(1);
    });
</script>
</body>

</html>
